﻿using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NinjaGoat
{
    class Rectangle
    {

        int vertexBuffer, indexBuffer;
        int numIndices;

        public void load()

        {
            Vertex[] vertices = new Vertex[5];
            vertices[0].Position = new Vector3(-.5f, -.5f, 0);
            vertices[1].Position = new Vector3(-.5f, .5f, 0);
            vertices[2].Position = new Vector3(.5f, .5f, 0);
            vertices[3].Position = new Vector3(.5f, -.5f, 0);
            vertices[4].Position = new Vector3(0, 0, 0);

            vertices[4].Color = new Vector3(0.5f, 0, 1);

            int[] indices = new int[] { 0, 1, 4,  1, 2, 4,  2, 3, 4,  3, 0, 4};
            numIndices = indices.Length;

            vertexBuffer = GL.GenBuffer();
            indexBuffer = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);

            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(4 * 6 * vertices.Length), vertices, BufferUsageHint.StaticDraw);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(4 * indices.Length), indices, BufferUsageHint.StaticDraw);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);  

        }

        public void Bind()
        {

            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer);
            GL.InterleavedArrays(InterleavedArrayFormat.C3fV3f, 4 * 6, (IntPtr)0);

   
        }

        public void Draw(Entity entity)
        {
            GL.PushMatrix();
            GL.Translate(entity.Position.X, entity.Position.Y, 0);
            GL.Scale(entity.Scale.X, entity.Scale.Y, 0);
            GL.Rotate(entity.Rotation, 0, 0, 1);
            GL.DrawElements(BeginMode.Triangles, numIndices, DrawElementsType.UnsignedInt, 0);
            GL.PopMatrix();
        }
        
        public void Unbind()
        {
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }
        
        
    }
}
